package com.example.demo.service.imp;

import com.example.demo.service.WecomService;
import com.google.gson.Gson;
import lombok.extern.log4j.Log4j2;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.List;

@Log4j2
@Service
public class WecomServiceImp implements WecomService {

    private static final OkHttpClient client = new OkHttpClient();
    private static final Gson gson = new Gson();


    @Value("${wecom.isNotify}")
    private Boolean isNotify = false;

    @Override
    public void notify(List<String> urls, String content,Boolean isTest) {
//        如果没有告警群，退出
        if (urls == null || urls.isEmpty()) return;
        // 构建消息对象
        WeComMessage message = new WeComMessage();
        message.setMsgtype("markdown");
        WeComMessage.Markdown markdown = new WeComMessage.Markdown();
        markdown.setContent(content);
        message.setMarkdown(markdown);

        // 将消息对象转换为JSON字符串
        String jsonMessage = gson.toJson(message);
        log.info("jsonMessage" + jsonMessage);

        // 创建请求体
        RequestBody requestBody = RequestBody.create(jsonMessage, MediaType.get("application/json; charset=utf-8"));

        urls.forEach(url -> {
            // 发送POST请求
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();

            log.info("request" + request);
//            if (isTest) return;
            try (Response response = client.newCall(request).execute()) {
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code " + response);
                }
                System.out.println("Message sent successfully.");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    // 内部类表示消息对象
    private static class WeComMessage {
        private String msgtype;
        private Text text;
        private Markdown markdown;

        public String getMsgtype() {
            return msgtype;
        }

        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }

        public Text getText() {
            return text;
        }

        public void setText(Text text) {
            this.text = text;
        }

        public Markdown getMarkdown() {
            return markdown;
        }

        public void setMarkdown(Markdown markdown) {
            this.markdown = markdown;
        }

        // 文本消息
        public static class Text {
            private String content;

            public String getContent() {
                return content;
            }

            public void setContent(String content) {
                this.content = content;
            }
        }

        //        markdown 消息
        public static class Markdown {
            private String content;

            public String getContent() {
                return content;
            }

            public void setContent(String content) {
                this.content = content;
            }
        }
    }
}
